package com.pan.alg.niuke.link;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

public class RevertLinkList {

    public ListNode ReverseList(ListNode head) {
        if(null==head||head.next==null){
            return head;
        }

        ListNode currentHead=head;
        ListNode revertHead=null;
        ListNode currenNext=head.next;
        while(currentHead.next!=null){
            currentHead.next=revertHead;
            revertHead=currentHead;
            currentHead=currenNext;
            currenNext=currentHead.next;
        }
        currentHead.next=revertHead;
        revertHead=currentHead;
        return revertHead;
    }

    public boolean hasCycle(ListNode head) {

        if(head==null||head.next==null){
            return false;
        }
        ListNode slowIndex = head;
        ListNode fastIndex = head;
        while(slowIndex.next!=null){
            slowIndex=slowIndex.next;
            if(fastIndex.next!=null&&fastIndex.next.next!=null){
                fastIndex=fastIndex.next.next;
            }else{
                return false;
            }
            if(fastIndex==slowIndex){
                return true;
            }
        }
        return false;
    }

    public ListNode EntryNodeOfLoop(ListNode pHead) {
        if(pHead==null||pHead.next==null){
            return null;
        }

        Set<ListNode> listNodes = new HashSet<>();
        listNodes.add(pHead);
        ListNode current =pHead;
        while(current.next!=null){
            current = current.next;
            if(listNodes.contains(current)){
                return current;
            }else{
                listNodes.add(current);
            }
        }
        return null;
    }

    public static ListNode addInList (ListNode head1, ListNode head2) {
        if(head1==null||head2==null){
            return null;
        }
        // write code here
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        while(head1!=null){
            sb1.append(head1.val);
            head1=head1.next;
        }
        while (head2!=null){
            sb2.append(head2.val);
            head2=head2.next;
        }
        BigDecimal num1 = new BigDecimal(sb1.toString());
        System.out.println("num1:"+num1);
        BigDecimal num2 = new BigDecimal(sb2.toString());
        System.out.println("num2:"+num2);
        BigDecimal num3 = num1.add(num2);
        System.out.println("num3:"+num3);
        String num4 = num3.toString();
        char[] chars = num4.toCharArray();

        ListNode currentHead =null;
        for (int i = chars.length-1; i >=0; i--) {
            System.out.println(String.valueOf(chars[i]));
            ListNode listNode = new ListNode(Integer.valueOf(String.valueOf(chars[i])));
            listNode.next=currentHead;
            currentHead = listNode;
        }
        return currentHead;
    }

    public static void main(String[] args) {
        int[] num1 = {5,9,7,5,7,1,2,6,4,2,7,8,9,6,1,6,6,1,1,4,2,9,5,5,0,4,6,3,0,4,3,5,6,7,0,5,5,4,4,0};
        int[] num2 = {1,3,2,5,0,6,0,2,1,4,3,9,3,0,9,9,0,3,1,6,5,7,8,6,2,3,8,5,0,9,7,9,4,5,9,9,4,9,3,6};
        ListNode listNode1 = arrayToList(num1);
        ListNode listNode2 = arrayToList(num2);
        ListNode listNode = addInList(listNode1, listNode2);
        printLink(listNode);

    }

    public static ListNode arrayToList(int[] arr){
        ListNode currentHead = null;
        for (int i = arr.length-1; i >=0 ; i--) {
            ListNode current = new ListNode(arr[i]);
            current.next=currentHead;
            currentHead=current;

        }
        ListNode see=currentHead;
        while (see!=null){
            System.out.println(see.val);
            see=see.next;
        }
        return currentHead;
    }

    public static void printLink(ListNode head){

        StringBuilder sb = new StringBuilder();
        while (head!=null){
            sb.append(head.val).append("-->");
            head=head.next;
        }
        System.out.println(sb.toString());
    }
}
